﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using MySkin.Enums;
using System.Reflection;
using System.Windows.Forms;


namespace MySkin.Tools
{
    public class ResUtils
    {
        //通过图片名称反射获取bitmap对象
        public static Bitmap GetResAsImage(string name)
        {
            if (name == null || name == "")
            {
                return null;
            }
            return (Bitmap)Properties.Resources.ResourceManager.GetObject(name);  //根据名字获取资源文件对象
        }
        /// <summary>
        /// 根据反射获取图片流，并从指定流中创建该图片
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        public static Image GetImageFormResStream(string imagePath)
        {
            return Image.FromStream(Assembly.GetExecutingAssembly()
             .GetManifestResourceStream(MethodBase.GetCurrentMethod().DeclaringType.Namespace
             + "." + imagePath));
        }
        /// <summary>
        /// 设置窗体的可重绘区域，以及圆角矩形
        /// </summary>
        /// <param name="form"></param>
        /// <param name="radius"></param>
        public static void SetFormRoundRectRgn(Form form, int radius)
        {
            IntPtr hRgn = Win32Message.CreateRoundRectRgn(0, 0, form.Width + 1, form.Height + 1, radius, radius);
            //设置可重绘区域
            Win32Message.SetWindowRgn(form.Handle, hRgn, true);
            Win32Message.DeleteObject(hRgn);
        }
        /// <summary>
        /// 移动窗体
        /// </summary>
        /// <param name="form"></param>
        public static void MoveForm(Form form)
        {
            Win32Message.ReleaseCapture();
            Win32Message.SendMessage(form.Handle, Win32Message.WM_NCLBUTTONDOWN, Win32Message.HTCAPTION, 0);
        }
        public static Bitmap GetResWithState(string name, ControlState controlState)
        {
            Bitmap bitmap = GetResAsImage(name);
            if(bitmap==null)
            {
                return null;
            }
            int block = 0;
            switch (controlState)
            {
                case ControlState.Normal: block = 0; break;
                case ControlState.MouseOver: block = 1; break;
                case ControlState.MouseDown: block = 2; break;
                case ControlState.Disable: block = 3; break;
            }
            int width = bitmap.Width / 4;
            Rectangle rect = new Rectangle(block * width, 0, width, bitmap.Height);
            return bitmap.Clone(rect, bitmap.PixelFormat);  //返回一个与资源文件格式相同，大小为rect长方形的bitmap
        }
    }
}
